﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.UserData;
	import cn.antscript.airCMS.display.component.btnbase.AbstractIco;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.maclema.mysql.Connection;
	import com.maclema.mysql.events.MySqlErrorEvent;
	import flash.data.SQLConnection;
	import flash.data.SQLMode;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;
	
	/**
	 * ...
	 * @author 
	 */
	public class MYSQLManager extends Sprite
	{
		public static var dbFile:File = File.applicationDirectory.resolvePath("data/data");  
		
		public static var HOST:String;
		public static var PORT:int;
		public static var USER:String;
		public static var PASS:String;
		public static var DATABASE:String;
		
		private var _closeBTN:AbstractIco;
		
		private var _hostLabel:Label;
		private var _portLabel:Label;
		private var _userLabel:Label;
		private var _passLabel:Label;
		private var _databaseLabel:Label;
		
		private var _host:InputText;
		private var _port:InputText;
		private var _user:InputText;
		private var _pass:InputText;
		private var _database:InputText;
		
		private var _save:PushButton;
		
		private static var _instance:MYSQLManager;
		
		private var _connect:Connection;
		
		
		private var _localConnect:SQLConnection;
		
		public function MYSQLManager(privateClass:PrivateClass) 
		{
			_closeBTN = new AbstractIco(IcoBitmapdatas.Delete, "返回", this, 180, 160,false);
			_closeBTN.addEventListener(MouseEvent.CLICK, onClose);
			
			
			_hostLabel = new Label(this, 0, 0,"主   机：");
			_portLabel = new Label(this, 0, 30,"端   口：");
			_userLabel = new Label(this, 0, 60,"用户名：");
			_passLabel = new Label(this, 0, 90,"密   码：");
			_databaseLabel = new Label(this, 0, 120,"数据库：");
			
			_host = new InputText(this, 60, 0);
			_port = new InputText(this, 60, 30);
			_user = new InputText(this, 60, 60);
			_pass = new InputText(this, 60, 90);
			_database = new InputText(this, 60, 120);
			_save = new PushButton(this, 60, 160, "保存", onSave);
			_pass.password = true;
			
			_localConnect = new SQLConnection();
			
		}
		
		private function onClose(e:MouseEvent):void 
		{
			this.parent.removeChild(this);
		}
		public static function getInstance():MYSQLManager {
			if (_instance == null) {
				_instance = new MYSQLManager(new PrivateClass);
			}
			return _instance;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		//-------------------------test--------------------------
		private function onSave(e:MouseEvent):void
		{
				_connect = new Connection(_host.text, int(_port.text), _user.text, _pass.text, _database.text);
				_connect.addEventListener(Event.CONNECT, onContented);
				_connect.addEventListener(MySqlErrorEvent.SQL_ERROR, onErr);
				_connect.addEventListener(IOErrorEvent.IO_ERROR, onIOErr);
				_connect.connect();
				
				WaitBar.getInstance().show(stage);
			
			
		}
		
		private function onIOErr(e:IOErrorEvent):void 
		{
			_connect.removeEventListener(Event.CONNECT, onContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onErr);		
			_connect.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
			
			WaitBar.getInstance().hide();
			AlertPanel.getInstance().show("设置失败，请重试",stage);
			
		}
		private function onErr(e:MySqlErrorEvent):void 
		{
			_connect.disconnect();
			
			_connect.removeEventListener(Event.CONNECT, onContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onErr);		
			_connect.removeEventListener(IOErrorEvent.IO_ERROR, onIOErr);
			
			WaitBar.getInstance().hide();
			AlertPanel.getInstance().show("设置失败，请重试",stage);
		}
		private function onContented(e:Event):void 
		{
			_connect.removeEventListener(Event.CONNECT, onContented);
			_connect.removeEventListener(MySqlErrorEvent.SQL_ERROR, onErr);
			
			_connect.disconnect();
			
			
			_localConnect.open(dbFile, SQLMode.UPDATE);
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = _localConnect;
			
			stmt.text = "UPDATE sqlData SET host='"+_host.text+"', port='"+_port.text+"', user='"+_user.text+"', password='"+_pass.text+"', database='"+_database.text+"'";
			
			stmt.execute();
			_localConnect.close();
			
			UserData.DB_HOST=_host.text ;
			UserData.DB_PORT=int(_port.text);
			UserData.DB_USER = _user.text;
			UserData.DB_PASSWORD = _pass.text;
			UserData.DB_DATABASE=_database.text;
			
			
			WaitBar.getInstance().hide();
			AlertPanel.getInstance().show("设置成功", stage);
			hide();
		}	
		
		//----------------------------------------
		
		
		
		public function show(container:DisplayObjectContainer):void {
			container.addChild(getInstance());
			
			//getInstance().x = (stage.stageWidth - getInstance().width) / 2;
			//getInstance().y = (stage.stageHeight - getInstance().height) / 2;
			graphics.clear();
			graphics.beginFill(0x000000, 0.9);
			graphics.drawRect( -getInstance().x, -getInstance().y, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			
			
			if (dbFile.exists) {
				getData();
			}
			else {
				createDB();
			}
			
		}		
		private function hide():void
		{			
			dispatchEvent(new Event(Event.CLOSE));
			this.parent.removeChild(this);
		}
		
		
		
		
		//------------------------get data
		private function getData():void
		{
			
			_localConnect.open(dbFile, SQLMode.UPDATE);
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = _localConnect;
			stmt.text = "SELECT * FROM sqlData";
			stmt.execute();
			_localConnect.close();
			var result:SQLResult = stmt.getResult();
			
			_host.text = result.data[0]["host"];
			_port.text = result.data[0]["port"];
			_user.text = result.data[0]["user"];
			_pass.text = result.data[0]["password"];
			_database.text = result.data[0]["database"];
			
			
			
			WaitBar.getInstance().hide();


		}
		//-------------------------------------
		
		
		
		//----------------------create DB
		private function createDB():void {	
			
			WaitBar.getInstance().show(stage);
			
			_localConnect.addEventListener(SQLEvent.OPEN, onCreateOpen);  
			_localConnect.addEventListener(SQLErrorEvent.ERROR, onSQLiteErr); 
			_localConnect.open(dbFile, SQLMode.CREATE);
		}
		
		private function onCreateOpen(e:SQLEvent):void 
		{
			_localConnect.removeEventListener(SQLEvent.OPEN, onCreateOpen);  
			_localConnect.removeEventListener(SQLErrorEvent.ERROR, onSQLiteErr); 
			
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = _localConnect;
			stmt.text = "CREATE TABLE sqlData (host longvarchar, port longvarchar,user longvarchar,password longvarchar,database longvarchar)";
			stmt.execute();
			stmt.text = "INSERT INTO sqlData (host,port,user,password,database) VALUES ('主机','端口','用户名','密码','数据库')";
			stmt.execute();
			_localConnect.close();
			getData();
		}
		private function onSQLiteErr(e:SQLErrorEvent):void   
		{       
			_localConnect.removeEventListener(SQLEvent.OPEN, onCreateOpen);  
			_localConnect.removeEventListener(SQLErrorEvent.ERROR, onSQLiteErr); 
			_localConnect.close(); 
		}   

		//-----------------------------
		
		
		
		public function loadData():Boolean {
			_localConnect.open(dbFile, SQLMode.UPDATE);
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = _localConnect;
			stmt.text = "SELECT * FROM sqlData";
			stmt.execute();
			_localConnect.close();
			var result:SQLResult = stmt.getResult();
			
			UserData.DB_HOST = result.data[0]["host"];
			UserData.DB_PORT = result.data[0]["port"];
			UserData.DB_USER = result.data[0]["user"];
			UserData.DB_PASSWORD = result.data[0]["password"];
			UserData.DB_DATABASE = result.data[0]["database"];
			
			return result==null;
		}
		
		
		
		
		
	}

}

class PrivateClass {
	
}